package org.markwebb.mina.playground.hbfilter.server;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;


/**
 * {@link IoFilter} implementation that sends a message
 * to the remote client once the session becomes idle.
 * 
 */
public class HeartbeatIdleFilter extends IoFilterAdapter {

	private ByteBuffer heartbeat;

	public HeartbeatIdleFilter(ByteBuffer hb) {
		this.heartbeat = ByteBuffer.allocate(hb.capacity());
		this.heartbeat.put( hb );
		this.heartbeat.flip();
	}

	@Override
	public void sessionIdle(NextFilter nextFilter, IoSession session,
			IdleStatus status) throws Exception {
		
		session.write(heartbeat);
		nextFilter.sessionIdle(session, status);
	}
}
